import os
import time
import datetime
import threading

def measure(cmd):
    ''' measure execuation time '''
    start = time.time()
    ret = os.popen(cmd)
    print ret.next().strip()
    stop = time.time()
    print start, stop, stop-start

def foo():
    ''' peroidic call without drift '''
    for i in range(10):
        print datetime.datetime.now()
        time.sleep(0.01)

def bar():
    ''' peroidic call with drift '''
    next_sleep = time.time()
    for i in range(10):
        print datetime.datetime.now()
        next_sleep += 0.01
        duration = next_sleep - time.time()
        time.sleep(duration)

def adjust(func):
    def wrappedFunc():
        
        return func()

def dry_timer(func):
    timer = threading.Thread(target=func)
    timer.daemon = True
    timer.start()

def catline(name):
    f = open(name)
    print f.next()

# define a deco
def measure2(func):
    def wrappedFunc(*args, **kwargs):
        start = time.time()
        ret = func(*args, **kwargs)        # save funcion return value
        stop = time.time()
        print stop - start
        return ret;          # return it again
    return wrappedFunc      # return wrapped function
